package org.lq.shopping.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.lq.shopping.dao.ShopGoodsDao;
import org.lq.shopping.dao.impl.ShopGoodsDaoImpl;
import org.lq.shopping.entity.ShopGoods;
import org.lq.shopping.service.ShopGoodsService;

import com.google.gson.Gson;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class ShopGoodsServiceImpl implements ShopGoodsService {
	
	
	static JedisPoolConfig config = new JedisPoolConfig();
	
	public static Jedis getJedis() {
		//控制最大连接数
		config.setMaxTotal(50);
		//设置空闲连接
		config.setMaxIdle(5);
		//默认-1,等待时间,如果等待时间内没有获得连接,之间抛出异常
		config.setMaxWaitMillis(1000*100);
		//在获取连接之前,validate操作,验证  保证得到连接可用
		config.setTestOnBorrow(true);
		//当返回一个连接pool 的时候先进行ping() 判断是否可以连接
		config.setTestOnReturn(true);
		//配置接口密码
		JedisPool pool = new  JedisPool(config,"127.0.0.1",6379);
		//一个redis连接对对象
		Jedis jedis = pool.getResource();
		jedis.select(1);
		return jedis;
	}
	
	ShopGoodsDao dao=new ShopGoodsDaoImpl();
	Gson gson = new Gson();
	@Override
	public boolean addGoods(ShopGoods goods) {
		goods.setGoods_img(goods.getGoods_imgList().toString());
		goods.setGoods_detail(gson.toJson(goods.getGoods_detailMap()));
		return dao.add(goods)>0;
	}

	@Override
	public boolean deleteGoods(int gid) {
		return dao.delete(gid)>0;
	}

	@Override
	public boolean updataGoods(ShopGoods goods) {
		goods.setGoods_img(goods.getGoods_imgList().toString());
		goods.setGoods_detail(gson.toJson(goods.getGoods_detailMap()));
		return dao.update(goods)>0;
	}

	@Override
	public List<ShopGoods> findGoodsPageByName(String name, int pageIndex, int pageSize) {
		List<ShopGoods> list = dao.getGoodsLikeName(name);

		Jedis jedis = getJedis();

		ShopGoods sCar = new ShopGoods();

		if (jedis.exists("shopgoods_name")==false) {
			for(ShopGoods s :list) {
				String s3 =gson.toJson(s);
				jedis.zadd("shopgoods_name", s.getGoods_id(), s3);
			}
		}

		List<String> listPage = (List<String>) jedis.zrangeByLex("shopgoods_name", "-", "+",(pageIndex -1)*pageSize, pageIndex*pageSize);
		List<ShopGoods> shopgoods = new ArrayList<>();
		for (String str : listPage) {
			ShopGoods s = gson.fromJson(str,ShopGoods.class);
			String img = s.getGoods_img();
			s.setGoods_imgList(Arrays.asList(img.substring(1, img.length()-1).split(",")));
			s.setGoods_detailMap(gson.fromJson(s.getGoods_detail(), Map.class));
			shopgoods.add(s);
		}
		return shopgoods;

	}

	@Override
	public List<ShopGoods> findGoodsPageByPrice(double min, double max, int pageIndex, int pageSize) {
		List<ShopGoods> list = dao.findGoodsPageByPrice(min, max);
		Jedis jedis = getJedis();

		ShopGoods sCar = new ShopGoods();

		if (jedis.exists("shopgoods_price")==false) {
			for(ShopGoods s :list) {
				String s3 =gson.toJson(s);
				jedis.zadd("shopgoods_price", s.getGoods_id(), s3);
			}
		}

		List<String> listPage = (List<String>) jedis.zrangeByLex("shopgoods_price", "-", "+",(pageIndex -1)*pageSize, pageIndex*pageSize);
		List<ShopGoods> shopgoods = new ArrayList<>();
		for (String str : listPage) {
			ShopGoods s = gson.fromJson(str,ShopGoods.class);
			String img = s.getGoods_img();
			s.setGoods_imgList(Arrays.asList(img.substring(1, img.length()-1).split(",")));
			s.setGoods_detailMap(gson.fromJson(s.getGoods_detail(), Map.class));
			shopgoods.add(s);
		}
		return shopgoods;
	}

	@Override
	public List<ShopGoods> findGoodsPageByType(int goodstypeid, int pageIndex, int pageSize) {
		List<ShopGoods> list = dao.findGoodsByType(goodstypeid);
		Jedis jedis = getJedis();

		ShopGoods sCar = new ShopGoods();

		if (jedis.exists("shopgoods_type")==false) {
			for(ShopGoods s :list) {
				String s3 =gson.toJson(s);
				jedis.zadd("shopgoods_type", s.getGoods_id(), s3);
			}
		}

		List<String> listPage = (List<String>) jedis.zrangeByLex("shopgoods_type", "-", "+",(pageIndex -1)*pageSize, pageIndex*pageSize);
		List<ShopGoods> shopgoods = new ArrayList<>();
		for (String str : listPage) {
			ShopGoods s = gson.fromJson(str,ShopGoods.class);
			String img = s.getGoods_img();
			s.setGoods_imgList(Arrays.asList(img.substring(1, img.length()-1).split(",")));
			s.setGoods_detailMap(gson.fromJson(s.getGoods_detail(), Map.class));
			shopgoods.add(s);
		}
		return shopgoods;
	}

	@Override
	public List<ShopGoods> findGoodsPageByBrand(String brand, int pageIndex, int pageSize) {
		List<ShopGoods> list = dao.findGoodsPageByBrand(brand);
		Jedis jedis = getJedis();

		ShopGoods sCar = new ShopGoods();

		if (jedis.exists("shopgoods_brand")==false) {
			for(ShopGoods s :list) {
				String s3 =gson.toJson(s);
				jedis.zadd("shopgoods_brand", s.getGoods_id(), s3);
			}
		}

		List<String> listPage = (List<String>) jedis.zrangeByLex("shopgoods_brand", "-", "+",(pageIndex -1)*pageSize, pageIndex*pageSize);
		List<ShopGoods> shopgoods = new ArrayList<>();
		for (String str : listPage) {
			ShopGoods s = gson.fromJson(str,ShopGoods.class);
			String img = s.getGoods_img();
			s.setGoods_imgList(Arrays.asList(img.substring(1, img.length()-1).split(",")));
			s.setGoods_detailMap(gson.fromJson(s.getGoods_detail(), Map.class));
			shopgoods.add(s);
		}
		return shopgoods;
	}

	@Override
	public List<ShopGoods> findPage(int pageIndex, int pageSize) {
		List<ShopGoods> list = dao.findAll();
		Jedis jedis = getJedis();

		ShopGoods sCar = new ShopGoods();

		if (jedis.exists("shopgoods_all")==false) {
			for(ShopGoods s :list) {
				String s3 =gson.toJson(s);
				jedis.zadd("shopgoods_all", s.getGoods_id(), s3);
			}
		}

		List<String> listPage = (List<String>) jedis.zrangeByLex("shopgoods_all", "-", "+",(pageIndex -1)*pageSize, pageIndex*pageSize);
		List<ShopGoods> shopgoods = new ArrayList<>();
		for (String str : listPage) {
			ShopGoods s = gson.fromJson(str,ShopGoods.class);
			String img = s.getGoods_img();
			s.setGoods_imgList(Arrays.asList(img.substring(1, img.length()-1).split(",")));
			s.setGoods_detailMap(gson.fromJson(s.getGoods_detail(), Map.class));
			shopgoods.add(s);
		}
		return shopgoods;
	}

	@Override
	public int getCount() {
		return dao.getCount();
	}

	@Override
	public int getCountByName(String name) {
		return dao.getCountByName(name);
	}

	@Override
	public int getCountByPrice(double min, double max) {
		return dao.getCountByPrice(min, max);
	}

	@Override
	public int getCountByType(int typeid) {
		return dao.getCountByType(typeid);
	}

	@Override
	public int getCountByBrand(String barnd) {
		return dao.getCountByBrand(barnd);
	}

}
